package com.snjgm.chapter1_custom_view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.security.MessageDigest;

/**
 * @author: snjgm
 * data: 2022/9/7
 * description: 自定义view,暂时停止在ondraw的学习
 *
 */
public class CustomView extends View {
    private String mText;
    private int mTextSize = 15;
    private int mTextColor = Color.BLACK;
    private Paint mPaint;


    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView);

        mText = array.getString(R.styleable.CustomView_text);
        mTextColor = array.getColor(R.styleable.CustomView_textColor, mTextColor);
        mTextSize = array.getDimensionPixelSize(R.styleable.CustomView_textSize, sp2px(mTextSize));

        array.recycle();
        mPaint = new Paint();
        //抗锯齿
        mPaint.setAntiAlias(true);
        //设置字体的大小和颜色
        mPaint.setColor(mTextColor);
        mPaint.setTextSize(mTextSize);

    }
    /**
      *
      * @description 将默认的15像素转为dp
      * @param 
      * @return 
      * author snjgm
      * time 2022/9/8 上午10:52
      */
    private int sp2px(int sp){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,sp,getResources().getDisplayMetrics());
    }

    /**
     * @param //AT_Most:在布局中指定了wrap_content //EXACTLY:在布局中指定了确切的值:100dp match_parent fill_parent
     *                                      //UNSPECIFIED:尽可能的大,很少有人用到,ListView,ScrollView
     *                                      //ScrollView + ListView会显示不全
     * @return author snjgm
     * time 2022/9/7 上午10:48
     * @description
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取宽高的模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //1、确定的值， 
        int width = MeasureSpec.getSize(heightMeasureSpec);

        //2、给定的是wrap_content 需要计算
        if(widthMode == MeasureSpec.AT_MOST){
            Rect bounds = new Rect();
            //获取文本中的Rect
            mPaint.getTextBounds(mText,0,mText.length(),bounds);
            width = bounds.width();
        }

        int height = MeasureSpec.getSize(heightMeasureSpec);

        if(heightMode == MeasureSpec.AT_MOST){
            Rect bounds = new Rect();
            //获取文本中的Rect
            mPaint.getTextBounds(mText,0,mText.length(),bounds);
            height = bounds.height();
        }
        //设置控件的宽高
        setMeasuredDimension(width,height);


    }

    /**
     * @param
     * @return author snjgm
     * time 2022/9/7 下午2:46
     * @description 画图
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

    /**
     * @param
     * @return author snjgm
     * time 2022/9/7 下午2:49
     * @description 处理和用户交互, 如手指交互等等, 事件分发拦截
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}
